home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / dev / misc / ImageFXDevKit.lha / sdev / sas / libsrc / start.c < prev   
Encoding:
C/C++ Source or Header  |  1992-10-02  |  4.7 KB  |  191 lines

  1. /*
  2.  * Standard Hook Startup Code, to make life easier.
  3.  *
  4.  * To keep hook program sizes down, I recommend using the following
  5.  * program and link steps for your hook (but of course you can do
  6.  * whatever you want to):
  7.  *
  8.  *    lc -v HookProgram
  9.  *    blink DEFINE __main=__tinymain SC SD ND
  10.  *          FROM LIB:c.o+start.o+HookProgram.o
  11.  *          TO HookProgram
  12.  *          LIB LIB:mirage.lib+LIB:amiga.lib+LIB:lc.lib
  13.  */
  14.  
  15. #include <exec/types.h>
  16. #include <exec/execbase.h>
  17. #include <intuition/intuition.h>
  18. #include <clib/dos_protos.h>
  19. #include <clib/exec_protos.h>
  20. #include <clib/intuition_protos.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <stdio.h>
  24. #include <scan/hooks.h>
  25.  
  26. /* C startup provides this one... */
  27. extern struct ExecBase  *SysBase;
  28.  
  29. /* Scan function library base... */
  30. struct ScanBase         *ScanBase = NULL;
  31.  
  32. /* These are supplied by Scan... */
  33. struct IntuitionBase    *IntuitionBase = NULL;
  34. struct GfxBase          *GfxBase = NULL;
  35.  
  36. /* To know whether we have called InitHook() or not... */
  37. static BOOL              hooked_in;
  38.  
  39. /* Our old current directory... */
  40. static BPTR              old_cd;
  41.  
  42. /* Old task name... */
  43. static char             *old_name;
  44.  
  45. /* Hook program defines these... */
  46. extern char             *HookName;
  47. extern char             *HookText;
  48. extern int               HookTextCount;
  49.  
  50. char                    **HookTextArray = NULL;
  51.  
  52. static char             **TextArray = NULL;
  53.  
  54. /*
  55.  * Exit the hook, cleaning up as we go.  Should be used in place of
  56.  * the standard exit() call by the main Hook code.
  57.  */
  58. void hook_exit (int rc)
  59. {
  60.    struct Task *task;
  61.  
  62.    if (hooked_in) {
  63.       if (HookTextArray) FreeText(HookTextArray, HookTextCount);
  64.       CurrentDir(old_cd);     /* back to our old current directory */
  65.       CleanupHook();
  66.       CloseLibrary((struct Library *)ScanBase);
  67.    }
  68.  
  69.    task = FindTask(NULL);
  70.    task->tc_Node.ln_Name = old_name;
  71.  
  72.    exit(rc);
  73. }
  74.  
  75. /*
  76.  * Can be called by the main Hook code, produces an error message and
  77.  * then exits the hook, cleaning up as appropriate.
  78.  */
  79. void hook_fail (char *msg)
  80. {
  81.    struct EasyStruct easy;
  82.    BOOL we_opened_intui = FALSE;
  83.  
  84.    if (IntuitionBase == NULL) {
  85.       IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 0);
  86.       we_opened_intui = TRUE;
  87.    }
  88.  
  89.    if (ScanBase) {
  90.  
  91.       /* If library open, use a scan call... */
  92.       Errorf(msg);
  93.  
  94.    }
  95.    else {
  96.  
  97.       if (SysBase->LibNode.lib_Version >= 36) {
  98.          /* Under 2.0 do a pleasant EasyRequest */
  99.          easy.es_StructSize = sizeof(struct EasyStruct);
  100.          easy.es_Flags = 0;
  101.          easy.es_Title = HookName;
  102.          easy.es_TextFormat = "%ls: %ls";
  103.          easy.es_GadgetFormat = "Okay";
  104.          EasyRequest(NULL, &easy, NULL, HookName, msg);
  105.       }
  106.       else {
  107.          /* Will use a jarring alert under 1.3 I guess... */
  108.          char buf[80];
  109.          int le;
  110.          buf[0] = 0;
  111.          buf[1] = 10;
  112.          buf[2] = (50-8)/2+6;
  113.          sprintf(&buf[3], "%ls: %ls", HookName, msg);
  114.          le = (640 - (strlen(&buf[3]) * 8)) / 2;
  115.          buf[strlen(&buf[3])+4] = '\0';
  116.          buf[0] = (le >> 8);
  117.          buf[1] = le & 255;
  118.          DisplayAlert(0x00000000L, buf, 50);
  119.       }
  120.  
  121.    }
  122.  
  123.    if (IntuitionBase && we_opened_intui)
  124.       CloseLibrary((struct Library *)IntuitionBase);
  125.  
  126.    hook_exit(20);
  127. }
  128.  
  129. /*
  130.  * Hook entry point... this does a little initialization and then calls
  131.  * the main hook entry point, called hook_main().
  132.  *
  133.  * Montage will call us with the name of Montage's function library
  134.  * in argv[1], and arguments to the hook in argv[2] and beyond.
  135.  *
  136.  */
  137. void main (int argc, char **argv)
  138. {
  139.    struct Task *task;
  140.  
  141.    hooked_in = FALSE;
  142.  
  143.    task = FindTask(NULL);
  144.  
  145.    old_name = task->tc_Node.ln_Name;
  146.    task->tc_Node.ln_Name = "ImageFX Hook";
  147.  
  148.    if (ScanBase == NULL) {
  149.       ScanBase = (struct ScanBase *)OpenLibrary(argv[1], 0);
  150.       if (ScanBase == NULL) {
  151.          /* just in case we're using an old Montage: */
  152.          ScanBase = (struct ScanBase *)OpenLibrary("monty1.library", 0);
  153.          if (ScanBase == NULL)
  154.             hook_fail("Cannot open ImageFX function library.");
  155.       }
  156.    }
  157.  
  158.    if (!InitHook()) hook_fail("Cannot communicate with ImageFX.");
  159.  
  160.    /*
  161.     * Change this hook's current directory to that of Mirage.
  162.     */
  163.    old_cd = CurrentDir(ScanBase->sb_CurrentDir);
  164.  
  165.    hooked_in = TRUE;
  166.  
  167.    IntuitionBase = ScanBase->IntuitionBase;
  168.    GfxBase = ScanBase->GfxBase;
  169.  
  170.    if (HookText) {
  171.       HookTextArray = ReadText(HookText, HookTextCount);
  172.    }
  173.  
  174.    hook_main(argc - 1, &argv[1]);
  175.    hook_exit(0);
  176. }
  177.  
  178.  
  179. /*
  180.  * GetStr() - Returns the text associated with a given string index
  181.  *            if available, otherwise returns the default text.
  182.  */
  183. char *GetStr (int index, char *def)
  184. {
  185.    if (HookTextArray)
  186.       return(HookTextArray[index]);
  187.    else
  188.       return(def);
  189. }
  190.  
  191.